﻿using MediatR;
using System.WXT.Domain;
using System.WXT.Domain.DTO;
using System.WXT.Infrstructre.Interface;
using System.WXT.WebAPI.Application.Command.CraftCommand;

namespace System.WXT.WebAPI.Application.CommandHandlers.CraftCommandHandlers
{
    public class CraftShowCommandHandlers:IRequestHandler<CraftShowCommand, PageDTO<Craft>>
    {
        protected readonly ICraftRepository craftRepository;

        public CraftShowCommandHandlers(ICraftRepository craftRepository)
        {
            this.craftRepository = craftRepository;
        }
        /// <summary>
        /// 工艺显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<PageDTO<Craft>> Handle(CraftShowCommand request, CancellationToken cancellationToken)
        {
            PageDTO<Craft> page = new PageDTO<Craft>();
            var list = (await craftRepository.GetAll()).Where(x => x.craftManageIsDel == 0);
            if(!string.IsNullOrEmpty(request.craftManagenumbering))
            {
                list = list.Where(x => x.craftManagenumbering == request.craftManagenumbering);
            }
            if(!string.IsNullOrEmpty(request.craftManageName))
            {
                list = list.Where(x => x.craftManageName == request.craftManageName);
            }
            if(request.craftManageState>0)
            {
                list = list.Where(x => x.craftManageState == request.craftManageState);
            }
            page.listcount = list.Count();
            page.pagecount = (int)Math.Ceiling(list.Count() * 1.0 / request.pageSize);
            page.plist = list.OrderByDescending(x=>x.craftManageId).Skip((request.pageIndex-1)*request.pageSize).Take(request.pageSize).ToList();
            return page;

        }
    }
}
